var express = require('express');
var router = express.Router();
var request = require('request');
var qs = require('qs');
var path = require('path');

router.get("/",function(req,res) {
    if(req.session.token) {
        res.sendFile(path.join(__dirname,"../src/templates/changePassword.html"));
    }else {
        res.redirect("/");
    }
});

router.post('/changepwd',function(req,res) {
    if(req.session.token) {
        var url = "http://39.106.9.239/ATM/user/changepwd";
        var data = {
            "oldpwd" : req.body.oldpass,
            "newpwd1" : req.body.newpass,
            "newpwd2" : req.body.renew
        }
       request({
           url : url,
           method : "post",
           headers : {
            "content-type" : "application/x-www-form-urlencoded",
            "authorization" : req.session.token,
            "deviceNo" : 100, 
           },
           body: qs.stringify(data)
       },function(_err,_res,_resBody) {
        if(!_err && _res.statusCode == 200) {
            res.send(_resBody);
         }else {
             console.log(_err);
         }
       });
    }else {
        res.redirect("/");
    }
});
router.get('/successpass',function(req,res) {
    if(req.session.token) {
        req.session.destroy();
        res.sendFile(path.join(__dirname,"../src/templates/successpass.html"));
    }else {
        res.redirect("/");
    }
});
module.exports = router;